
$(function(){
	play_index.run();	
});

var play_index = {	
		run: function(){
			this.__init__();			
			this._start_();
			this._bind_events();
		},
		
		__init__: function(){
			this.contextPath = $("#contextPath").val() || "";					
			this.itemUrl = $("#itemUrl").val();
			this.id = $("#id").val() || '';		
			this.title = $("#title").val() || '';			
			this.category = $("#category").val();
			this.website = $("#website").val();
			this.vdInstalled = vd.check();
		},
		
		_start_: function(){	
			main._play_close_side();
			//main.addOrUpdateViewHistory(play_index.category,play_index.title,play_index.id);
			
			var loc = location.href;
			var index = loc.indexOf("#");
			if(index > 0){
				var params = loc.substring(index + 1);
				this._highlight_genre(params);
			}
			
			play_index._highlight_nav();
			play_index._get_relate_video();
		},
		
		_bind_events: function(){			
			//genre
			$("#genres_ul > li > a").click(function(){
				var $this = $(this);
				var g = $this.attr("genre");
				location.href = main.contextPath + "/world/movie/" + play_index.category + "s/#genre=" + g; 
				return false;
			});
			
			var locaIndex = location.href.indexOf("#"); 
			var localUrl = location.href;
			if(locaIndex > 0){
				localUrl = localUrl.substring(0,locaIndex);
			}
			$("#fb-comments").attr("data-href", localUrl);
			$(".loadingBox").show();
		},
		
		_get_relate_video: function(){						
			var url = main.contextPath + "/world/movie/full-length/relate/" + this.id;
			$.get(url, function(result){
				$("#other_video").html(result.trim());
			});
		},
		
		_highlight_nav: function(){
			var index = 0;
			if(this.category == 'trailer'){
				index = 1;
			}
			$("#side_menu > ul > li").eq(index).addClass("curr");			
		},
		
		_highlight_genre: function(params){
			var paramArr = params.split("&");
			for(var i=0; i<paramArr.length;i++){
				if (paramArr[i].indexOf("genre") >= 0) {
					var value = paramArr[i].substring(paramArr[i].indexOf("=") + 1);
					$("#genres_ul > li > a").removeClass("selected");
					$("#genres_ul > li > a").each(function() {
						if ($(this).attr("genre") == decodeURI(value)) {
							$(this).addClass("selected");
						}
					});
				}
			}
		}
		
};
